package com.tj.ocr.similar;

import java.awt.image.BufferedImage;
import java.io.Serializable;

import com.tj.ocr.img.ImageRW;
import com.tj.ocr.img.MyImage;

public class FingerPrint implements Serializable{
	private static final long serialVersionUID = -8576306444633455014L;

	private int id = 0;
	private int id_t = 0;
	private int[] charactor;
	private String text;
	private int height;
	
	public static FingerPrint get(BufferedImage img) {
		FingerPrint rst = new FingerPrint();
		// get project
		rst.charactor = Project.filter(img);
		// get id
		rst.id = encodeCharactor(rst.charactor, img.getHeight());
		rst.setId_t(encodeCharactor2(rst.charactor, img.getHeight()));
		rst.height = img.getHeight();
		
		return rst;
	}

	public static FingerPrint getEx(MyImage img, int skip) {
		FingerPrint rst = new FingerPrint();
		// get project
		rst.charactor = Project.filter(img);
		// get id
		rst.id = encodeCharactor(rst.charactor, skip);
		rst.setId_t(encodeCharactor2(rst.charactor, skip));
		rst.height = skip;
		
		return rst;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public int getId() {
		return id;
	}

	public boolean isSame(FingerPrint fp) {
		return fp.getId_t() == this.id_t;
	}

	public void setId(int id) {
		this.id = id;
	}

	public static int encodeCharactor2(int[] charactors, int skip) {
		int ret = 0, hi = 0;
		for (int i = skip - 1; i > -1; i--) {
			if (charactors[i] > 0) {
				ret = ret * (hi++) + charactors[i];
			}
		}

		return ret;

		/*
		 * int index = 1, ret = 0; for(int f : charactors){ ret = ret*index + f;
		 * index = index +1; }
		 * 
		 * return ret;
		 */
	}

	public static int encodeCharactor(int[] charactors, int skip) {
		int ret = 0;
		for (int i = skip; i < charactors.length; i++) {
			ret = ret * (skip - i) + charactors[i];
		}

		return ret;
	}

	public static double sim_distance(int[] vector1, int[] vector2) {
		double distance = 0;

		if (vector1.length == vector2.length) {
			for (int i = 0; i < vector1.length; i++) {
				double temp = Math.pow((vector1[i] - vector2[i]), 2);
				distance += temp;
			}
			distance = Math.sqrt(distance);
		}
		//System.out.println(distance);
		
		return distance;
	}

	public static void main(String[] args) {
		FingerPrint r = get(ImageRW.read("d:\\test\\1r.jpg"));
		System.out.println(r.getId());
	}
	
	public int getId_t() {
		return id_t;
	}

	public void setId_t(int id_t) {
		this.id_t = id_t;
	}

	public boolean isSimilar(FingerPrint print) {
		return sim_distance(this.zipCharactor(), print.zipCharactor()) > 0.9;
	}

	private int[] zipCharactor() {
		int posf = 0, post = 0;
		for(int i=0;i<height;i++){
			if(this.charactor[i] != 0){
				posf = i;
				break;
			}
		}
		for(int i=height-1;i>-1;i--){
			if(this.charactor[i] != 0){
				post = i;
				break;
			}
		}
		
		if(post == 0)
			post --;
		//pos += height;
		int [] ret = new int[post - posf + 1 + this.charactor.length - height];
		for(int i=posf; i<=post; i++){
			ret[i-posf] = this.charactor[i];
		}
		for(int i=height;i<this.charactor.length;i++){
			ret[post - posf + 1 + i - height] = this.charactor[i];
		}
		
		return ret;
	}

	public int getRowId() {
		int ret = 0;
		for (int i = height; i < this.charactor.length; i++) {
			ret += charactor[i];
		}

		return ret;
	}
	
	public int getColId() {
		int ret = 0;
		for (int i = 0; i < height; i++) {
			ret += charactor[i];
		}

		return ret;	}
}
